package com.xiaolin.sports.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaolin.sports.common.Constants;
import com.xiaolin.sports.common.RspResult;
import com.xiaolin.sports.common.enums.Status;
import com.xiaolin.sports.model.Grid;
import com.xiaolin.sports.model.SchoolDisCourse;
import com.xiaolin.sports.model.SchoolInfo;
import com.xiaolin.sports.model.SysUser;
import com.xiaolin.sports.page.Page;
import com.xiaolin.sports.reqVo.UpdateIdsVO;
import com.xiaolin.sports.service.ISchoolInfoService;
import com.xiaolin.sports.view.SchoolDisView;
import com.xiaolin.sports.vo.SchoolInfoQueryVO;

@RequestMapping("school")
@Controller
public class SchoolController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SchoolController.class);
	
	@Autowired
	private ISchoolInfoService schoolInfoService;
	
	@RequestMapping("manage")
	public String schoolManage(){
		return "school/manage";
	}
	
	@RequestMapping("addAndUpdate")
	@ResponseBody Object schoolAddAndUpdate(SchoolInfo record) {
		try {
			SysUser sysUser = (SysUser) session.getAttribute(Constants.APP_USER);
			schoolInfoService.addAndUpdateSchoolInfo(record, sysUser);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---schoolAddAndUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("batchDel")
	@ResponseBody Object batchDel(@RequestParam("ids[]")List<Integer> ids){
		try {
			SysUser sysUser = (SysUser) session.getAttribute(Constants.APP_USER);
			UpdateIdsVO vo = new UpdateIdsVO();
			vo.setIds(ids);
			vo.setUpdater(sysUser.getUserName());
			vo.setUpdateUserId(sysUser.getIdUser());
			schoolInfoService.updateBatchDel(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--------batchDel error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("json/query")
	@ResponseBody Object schoolQuery(SchoolInfoQueryVO vo) {
		try {
			Page<SchoolInfo> page = new Page<SchoolInfo>();
			Map<String, Object> paramMap = new HashMap<String, Object>();
			Grid grid = new Grid();
			paramMap.put("city", vo.getCity());
			paramMap.put("schoolName", vo.getSchoolName());
			page.setParams(paramMap);
			page.setPageNo(vo.getPage());
			if(vo.getRows() == -1) page.setPageSize(Integer.MAX_VALUE - 1);
			else page.setPageSize(vo.getRows());
			List<SchoolInfo> list = schoolInfoService.selectByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---schoolQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("course/manage")
	public String courseManage() {
		return "school/courseManage";
	}
	
	@RequestMapping("dis/course/addAndUpdate")
	@ResponseBody Object disCourseAddAndUpdate(SchoolDisCourse disCourse) {
		try {
			SysUser sysUser = (SysUser) session.getAttribute(Constants.APP_USER);
			schoolInfoService.addAndUpdateSchoolDis(disCourse, sysUser);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---disCourseAddAndUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("dis/course/query")
	@ResponseBody Object schoolDisCourseQuery(Integer schoolId) {
		try {
			Grid grid = new Grid();
			List<SchoolDisView> list = new ArrayList<>();
			if(schoolId != null) {
				list = schoolInfoService.selectSchoolDisList(schoolId);
			}
			grid.setRows(list);
			grid.setTotal(list.size());
			return grid;
		} catch (Exception e) {
			logger.error("-----schoolDisCourseQuery error={}", e);
		}
		return null;
	}
}
